package com.glory.wxelectricitypda.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.MarkQueryBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.TestProgramQueryBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class TestProgramDownloadActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.eqpID)
    FreeUI_ClearEditText eqpID;
    @BindView(R.id.testeqpId)
    FreeUI_ClearEditText testeqpId;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.testPlatform)
    TextView testPlatform;
    @BindView(R.id.recipeType)
    TextView recipeType;
    @BindView(R.id.pinName)
    TextView pinName;
    @BindView(R.id.gradingNo)
    TextView gradingNo;
    @BindView(R.id.programName)
    TextView programName;
    @BindView(R.id.programPath)
    TextView programPath;
    @BindView(R.id.woLotid)
    TextView woLotid;
    @BindView(R.id.customerCode)
    TextView customerCode;
    @BindView(R.id.recipeVar)
    TextView recipeVar;
    @BindView(R.id.equipmentId)
    TextView equipmentId;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_test_program_download;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("测试程序下载");
        freeUI_titleView.setRightListener("下载", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (eqpID.getText().toString().equals("")){
                    showToast("请扫描分选机",false);
                    return;
                }
                if (lotID.getText().toString().equals("")){
                    showToast("请扫描批次ID",false);
                    return;
                }
                if (testeqpId.getText().toString().equals("")){
                    showToast("请扫描测试机",false);
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("EqpID", eqpID.getText().toString());
                    jsonObject.put("TesteqpId", testeqpId.getText().toString());
                    jsonObject.put("LotID", lotID.getText().toString());
                    jsonObject.put("ProgramPath", programPath.getText().toString());
                    jsonObject.put("ProgramNmae", programName.getText().toString());
                    jsonObject.put("GradingNo", gradingNo.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostUtils("downLoadProgram","DownLoadProgram",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                        new TypeToken<ResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<Object>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("下载成功",true);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(), "DownLoadProgram");
                    }
                });

            }
        });
    }

    @Override
    protected void initData() {

        initEdit(lotID,s ->{

            if (eqpID.getText().toString().equals("")) {
                showToast("请扫描分选机", false);
                return;
            }
            if (testeqpId.getText().toString().equals("")) {
                showToast("请扫描测试机", false);
                return;
            }
            testProgramQuery();
        });
        initEdit(eqpID,s ->{

            if (lotID.getText().toString().equals("")) {
                showToast("请扫描批次ID", false);
                return;
            }
            if (testeqpId.getText().toString().equals("")) {
                showToast("请扫描测试机", false);
                return;
            }
            testProgramQuery();
        });
        initEdit(testeqpId,s ->{

            if (lotID.getText().toString().equals("")) {
                showToast("请扫描批次ID", false);
                return;
            }
            if (eqpID.getText().toString().equals("")) {
                showToast("请扫描分选机", false);
                return;
            }
            testProgramQuery();
        });

    }


    private void testProgramQuery(){


        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID.getText().toString());
            jsonObject.put("TesteqpId", testeqpId.getText().toString());
            jsonObject.put("LotID", lotID.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("programSelect","programSelect",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<TestProgramQueryBean>>(
                new TypeToken<ResponseBean<TestProgramQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<TestProgramQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("查询成功", true);
                    TestProgramQueryBean body = response.body().getBody();
                    testPlatform.setText(body.getTEST_PLATFORM());
                    recipeType.setText(body.getRECIPE_TYPE());
                    pinName.setText(body.getPIN_NAME());
                    gradingNo.setText(body.getGRADING_NO());
                    programName.setText(body.getPROGRAM_NAME());
                    programPath.setText(body.getPROGRAM_PATH());
                    woLotid.setText(body.getWO_LOTID());
                    customerCode.setText(body.getCUSTOMER_CODE());
                    recipeVar.setText(body.getRECIPE_VAR());
                    equipmentId.setText(body.getTESTEQP_ID());
                }
            }

            @Override
            public void onError(Response<ResponseBean<TestProgramQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "programSelect");
            }
        });


    }



    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}